package com.general.framework.example.sign;

import java.security.MessageDigest;

/**
 * 写一个MD5算法,运行结果与MySQL的md5()函数相同
 * 将明文密码转成MD5密码
 * 123456->e10adc3949ba59abbe56e057f20f883e
 */
public final class MD5Util {
	private MD5Util(){}
	/**
	 * 将明文密码转成MD5密码 
	 */
	public static String MD5Encode(String origin, String charset) throws Exception{
		String resultString = null;
		try {
			// resultString = new String(origin);
			// MessageDigest md = MessageDigest.getInstance("MD5");
			// if (charset == null || "".equals(charset))
			// 	resultString = byteArrayToHexString(md.digest(resultString
			// 			.getBytes()));
			// else
			// 	resultString = byteArrayToHexString(md.digest(resultString
			// 			.getBytes(charset)));
			MessageDigest algorithm = MessageDigest.getInstance("MD5");
			// algorithm.reset();
			// algorithm.update(resultString.getBytes("UTF-8"));
			// byte[] messageDigest = algorithm.digest();
			resultString = byteArrayToHexString(algorithm.digest(origin.getBytes(charset)));
			// return messageDigest;
		} catch (Exception exception) {
		}
		return resultString;
	}
	/**
	 * 将byte[]转在16进制字符串 
	 */
	private static String byteArrayToHexString(byte[] byteArray) {
		StringBuffer sb = new StringBuffer();
		// 遍历
		for(byte b : byteArray){// 16次
			// 取出每一个byte类型，进行转换
			String hex = byteToHexString(b);
			// 将转换后的值放入StringBuffer中
			sb.append(hex);
		}
		return sb.toString();
	}
	/**
	 * 将byte转在16进制字符串 
	 */
	private static String byteToHexString(byte b) {//-31转成e1，10转成0a，。。。
		// 将byte类型赋给int类型
		int n = b;
		// 如果n是负数
		if(n < 0){
			// 转正数
			//-31的16进制数，等价于求225的16进制数 
			n = 256 + n;
		}
		// 商(14)，数组的下标
		int d1 = n / 16;
		// 余(1)，数组的下标
		int d2 = n % 16;
		// 通过下标取值
		return hex[d1] + hex[d2];
	}
	private static String[] hex = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
}
